#!/bin/bash
set -ex

cur_dir=$(dirname "$(readlink -f "$0")")
mysql_root="/usr/local/mysql-8.0"

# shellcheck disable=SC2164
pushd "$cur_dir"
if [[ ! -d "$mysql_root" ]]; then
    echo "not found $mysql_root"
    mkdir -p $mysql_root/data
    mkdir -p $mysql_root/conf
    mkdir -p $mysql_root/logs

# 这里不允许缩进
cat > "$mysql_root/conf/my.cnf"<<EOF
[mysqld]
user=mysql
# 表示允许任何主机登陆MySQL
bind-address = 0.0.0.0
port=3306
default-storage-engine=INNODB
#character-set-server=utf8
character-set-client-handshake=FALSE
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci
init_connect='SET NAMES utf8mb4'
secure-file-priv=
[client]
default-character-set=utf8mb4
[mysql]
default-character-set=utf8mb4
EOF
fi
docker compose -f ./docker-compose.yml up -d --build mysql
# shellcheck disable=SC2164
popd